package com.ruoyi.system.service.vantservice.customerervice.impl;

import com.ruoyi.system.domain.customer.CustomerLogin;
import com.ruoyi.system.domain.customer.LocalFabulous;
import com.ruoyi.system.domain.customer.LocalPicture;
import com.ruoyi.system.domain.customer.LocalReviers;
import com.ruoyi.system.domain.zhuangxiumain.vo.ResviersVo;
import com.ruoyi.system.mapper.vant.customer.ApiLocalFabulousMapper;
import com.ruoyi.system.mapper.vant.customer.ApiLocalMapper;
import com.ruoyi.system.mapper.vant.customer.ApiLocalPictureMapper;
import com.ruoyi.system.service.vantservice.customerervice.ApiLocalFabulousService;
import com.ruoyi.system.utils.IdentifiedGenerator;
import com.ruoyi.system.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by 惠普 on 2020/12/30 17:21
 */
@Service("apiLocalFabulousService")
public class ApiLocalFabulousServiceImpl implements ApiLocalFabulousService {
    @Resource
    private ApiLocalFabulousMapper apiLocalFabulousMapper;
    @Autowired
    private ApiLocalMapper localReviersMapper;
    @Resource
    private ApiLocalPictureMapper apiLocalPictureMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private TokenUtils tokenUtils;

    /**
     * 添加点赞
     * @param localFabulous
     * @return
     */
    @Override
    public int addLocalFabulous(LocalFabulous localFabulous) {
        int row = 0;
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        localFabulous.setCustomerId(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
        LocalReviers localReviers = apiLocalFabulousMapper.selectLocalReviersById1(Long.parseLong(String.valueOf(localFabulous.getLocalReviersId())));
        localReviers.setLocalReviersFabulous((localReviers.getLocalReviersFabulous()+1));
        localReviersMapper.updateLocalReviersById(localReviers);
        row = apiLocalFabulousMapper.addLocalFabulous(localFabulous);
        return row;
    }

    /**
     * 消除点赞
     * @param localFabulous
     * @return
     */
    @Override
    public int updateLocalFabulous(LocalFabulous localFabulous) {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        localFabulous.setCustomerId(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
        LocalReviers localReviers = apiLocalFabulousMapper.selectLocalReviersById1(Long.parseLong(String.valueOf(localFabulous.getLocalReviersId())));
        if(localFabulous.getLocalFabulousStatus()==1){
            localReviers.setLocalReviersFabulous((localReviers.getLocalReviersFabulous()+1));
            localReviersMapper.updateLocalReviersById(localReviers);
        }else {
            localReviers.setLocalReviersFabulous((localReviers.getLocalReviersFabulous()-1));
            localReviersMapper.updateLocalReviersById(localReviers);
        }
        return apiLocalFabulousMapper.updateLocalFabulous(localFabulous);
    }

    /**
     * 查询点赞是否存在
     * @param localFabulous
     * @return
     */
    @Override
    public LocalFabulous selectLocalFabulousByTwoId(LocalFabulous localFabulous) {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        localFabulous.setCustomerId(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
        return apiLocalFabulousMapper.selectLocalFabulousByTwoId(localFabulous);
    }

    /**
     * 根据用户的id查询发布的信息多少条
     * @return
     */
    @Override
    public int selectLocalReviersByCustomerId() {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        return apiLocalFabulousMapper.selectLocalReviersByCustomerId(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
    }

    /**
     * 添加本地用户发布的信息
     * @param localReviers
     * @return
     */
    @Override
    public int addReviersByCustomer(LocalReviers localReviers) {
        System.out.println(localReviers);
        int row = 0;
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        localReviers.setLocalReviewsUser(customerLogin.getCustomerId());
        localReviers.setLocalReviersTime(new Date());
//        Integer u = IdentifiedGenerator.Num();
//        localReviers.setLocalPictureUuid(u);
        row = apiLocalFabulousMapper.addReviersByCustomer(localReviers);
        Integer u = Math.toIntExact(localReviers.getLocalReviewsId());
        if(row == 1){
            int i = 0;
            for (Object s : localReviers.getFileList()){
                for (Map<String,Object> map : localReviers.getUrlList()){
                    System.out.println(map.get(String.valueOf(s)));
                    if(map.get(String.valueOf(s))!=null && i==0){
                        localReviers.setImg0(String.valueOf(map.get(String.valueOf(s))));
                    }else if(map.get(String.valueOf(s))!=null && i==1){
                        localReviers.setImg1(String.valueOf(map.get(String.valueOf(s))));
                    }else if(map.get(String.valueOf(s))!=null && i==2){
                        localReviers.setImg2(String.valueOf(map.get(String.valueOf(s))));
                    }else if(map.get(String.valueOf(s))!=null && i==3){
                        localReviers.setImg3(String.valueOf(map.get(String.valueOf(s))));
                    }
                }
                i = i+1;
            }
            LocalPicture localPicture = new LocalPicture();
            localPicture.setLocalPictureUuid(u);
            if(localReviers.getImg0()!=null){
                localPicture.setLocalPictureUrl("http://51jhf.com/profile/xiazai"+localReviers.getImg0());
                apiLocalPictureMapper.addLocalPicture(localPicture);
            }
            if(localReviers.getImg1()!=null){
                localPicture.setLocalPictureUrl("http://51jhf.com/profile/xiazai"+localReviers.getImg1());
                apiLocalPictureMapper.addLocalPicture(localPicture);
            }
            if(localReviers.getImg2()!=null){
                localPicture.setLocalPictureUrl("http://51jhf.com/profile/xiazai"+localReviers.getImg2());
                apiLocalPictureMapper.addLocalPicture(localPicture);
            }
            if(localReviers.getImg3()!=null){
                localPicture.setLocalPictureUrl("http://51jhf.com/profile/xiazai"+localReviers.getImg3());
                apiLocalPictureMapper.addLocalPicture(localPicture);
            }
        }
        return row;
    }

    /**
     * 查询用户自己发布的全部本地信息
     * @param resviersVo
     * @return
     */
    @Override
    public List<ResviersVo> selectResviersByCustomer(ResviersVo resviersVo) {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        resviersVo.setLocalReviewsUser(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
        List<ResviersVo> list = apiLocalFabulousMapper.selectResviersByCustomer(resviersVo);
        for (ResviersVo resviersVo1 : list){
            List list1 = apiLocalPictureMapper.selectLocalPictureByUuid(resviersVo1.getLocalReviewsId());
            resviersVo1.setPictureList(list1);
        }
        return list;
    }

    /**
     * 上传多图片
     * @return
     */
    @Override
    public Map<String, Object> uploadImages(MultipartFile[] files) {
        Map<String, Object> map = new HashMap<>();
//        String url = "D:\\Java\\xiazai\\";
        String url = "/home/ruoyi/profile/xiazai";
        String fileName = null;
        String fileNameOne = "tu";
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                try {
                    //上传地址
                    File floader = new File(url);
                    if (!floader.exists()){
                        floader.mkdir();//如果不存在这个文件夹,就创建上
                    }
                    //获取文件名
                    fileName = files[i].getOriginalFilename();
//                    fileName = "tupian";
                    System.err.println("************fileName***********fileName***********"+fileName);
                    //文件输出流
                    FileOutputStream fileOutputStream = null;
                    String ns = fileName.substring(0, fileName.indexOf("."));//获取名字.之前的
                    System.err.println("*****************ns**********"+ns);
                    String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
                    String newFileNamePrefix = UUID.randomUUID().toString().replace("-", "") + new SimpleDateFormat("yyyyMMddHHssSSS").format(new Date());
                    String newFileName = newFileNamePrefix + suffix;
                    fileOutputStream = new FileOutputStream(url + newFileName);
                    //包装流
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                    //写入
                    bufferedOutputStream.write(files[i].getBytes());
                    //缓冲 关闭流
                    bufferedOutputStream.flush();
                    bufferedOutputStream.close();
//                    map.put("img"+String.valueOf(i),newFileName);//images/232132eadsa32332432.jpg
                    map.put(fileName,newFileName);//images/232132eadsa32332432.jpg
                    map.put("code",200);
                } catch (Exception e) {
                    e.printStackTrace();
                    map.put("code",500);
                    return map;
                }
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> uploadPingzheng(MultipartFile[] files) {
        Map<String, Object> map = new HashMap<>();
//        String url = "D:\\Java\\xiazai\\";
        String url = "/home/ruoyi/profile/xiazai";
        String fileName = null;
        String fileNameOne = "tu";
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                try {
                    //上传地址
                    File floader = new File(url);
                    if (!floader.exists()){
                        floader.mkdir();//如果不存在这个文件夹,就创建上
                    }
                    //获取文件名
                    fileName = files[i].getOriginalFilename();
//                    fileName = "tupian";
                    System.err.println("************fileName***********fileName***********"+fileName);
                    //文件输出流
                    FileOutputStream fileOutputStream = null;
                    String ns = fileName.substring(0, fileName.indexOf("."));//获取名字.之前的
                    System.err.println("*****************ns**********"+ns);
                    String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
                    String newFileNamePrefix = UUID.randomUUID().toString().replace("-", "") + new SimpleDateFormat("yyyyMMddHHssSSS").format(new Date());
                    String newFileName = newFileNamePrefix + suffix;
                    fileOutputStream = new FileOutputStream(url + newFileName);
                    //包装流
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
                    //写入
                    bufferedOutputStream.write(files[i].getBytes());
                    //缓冲 关闭流
                    bufferedOutputStream.flush();
                    bufferedOutputStream.close();
//                    map.put("img"+String.valueOf(i),newFileName);//images/232132eadsa32332432.jpg
                    map.put(fileNameOne,newFileName);//images/232132eadsa32332432.jpg
                    map.put("code",200);
                } catch (Exception e) {
                    e.printStackTrace();
                    map.put("code",500);
                    return map;
                }
            }
        }
        return map;
    }

    /**
     * 查询用户点赞的数量
     * @return
     */
    @Override
    public int selectLocalFabulousByCustomerId() {
        CustomerLogin customerLogin = tokenUtils.getLoginCustomer(request);
        return apiLocalFabulousMapper.selectLocalFabulousByCustomerId(Integer.parseInt(String.valueOf(customerLogin.getCustomerId())));
    }


}
